<template>
	<view class="container">
		<view class="form-box">
			<view class="form">
				
				<!-- <list-cell :hover="false">
					<view class="form-input">
						<view class="label">id</view> -->
						 <!-- <input class="input" v-model="form.id" placeholder-class="text-color-assist"/> -->
					<!-- </view>
				</list-cell> -->
				
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">收货人</view>
						 <input class="input" placeholder="请输入收货人" v-model="form.accept_name" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<!-- <list-cell :hover="false">
					<view class="form-input">
						<view class="label">性别</view>
						<view class="radio-group">
							<view class="radio" :class="{'checked': !form.sex}" style="margin-right: 10rpx;" @tap="form.sex=0">先生</view>
							<view class="radio" :class="{'checked': form.sex}" @tap="form.sex=1">女士</view>
						</view>
					</view>
				</list-cell> -->
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">手机号码</view>
						 <input class="input" placeholder="请输入收货人联系方式" v-model="form.mobile" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<list-cell :hover="false">
					<view class="form-input">
						<view class="label">详细地址</view>
						 <input class="input" placeholder="请选择收货地址" v-model="form.street" placeholder-class="text-color-assist"/>
					</view>
				</list-cell>
				<!-- <list-cell :hover="false">
					<view class="form-input">
						<view class="label">门牌号</view>
						 <input class="input" placeholder="请输入收货人详细地址" v-model="form.door_number" placeholder-class="text-color-assist"/>
					</view>
				</list-cell> -->
			</view>
			<view class="btn-section">
				<button type="primary" size="default" @tap="save">保存</button>
				
				<button type="primary" size="default" @tap="del">删除</button>
			</view>
		</view>
	</view>
</template>

<script>
	import listCell from '@/components/list-cell/list-cell'
	
	export default {
		components: {
			listCell
		},
		data() {
			return {
				index: '',
				form: {
					id:'0',
					accept_name: '',
					mobile: '',
					street: ''
				}
				
			}
		},
		// async onLoad({index,id}) {
			// async onLoad({id}) {
		async onLoad(option) {
			this.index = option.index;
			console.log(this.index);
			if(option.index == 'edit'){
				uni.showLoading({
					title:"加载中...."
				})
				uni.request({
					// url: 'https://unidemo.dcloud.net.cn/api/news',
					url: this.$apiUrl + '/clientAddress/getaddressesById',
					method: 'POST',
					header:{
						'Content-type':'application/x-www-form-urlencoded'
					},
					// data: {}, 
					data: {addressid:option.id},
					success: res => {
						this.form = res.data;
						uni.hideLoading();
					}, 
					fail: () => {},
					complete: () => {}
				});
			}
			
			
			
		// 	//为了方便演示，这里用本地缓存
		// 	if(id) {
		// 		const addresses = uni.getStorageSync('addresses')
		// 		this.form = addresses.find(item => item.id == id)
		// 	}
		},
		methods: {
			save() {
				// uni.navigateBack()
				let data = this.form;
				uni.showLoading({
					title:"加载中...."
				})
				uni.request({
					// url: 'https://unidemo.dcloud.net.cn/api/news',
					url: this.$apiUrl + '/clientAddress/saveAddress',
					method: 'POST',
					header:{
						'Content-type':'application/x-www-form-urlencoded'
					},
					// data: {}, 
					data: data,
					success: res => {
						this.addresses = res.data;
						uni.hideLoading();
					}, 
					fail: () => {},
					complete: () => {}
				});
				
				//返回上一页并刷新
				this.refreshBackPage();
				
				
			},
			del() {
				// uni.navigateBack()
				// let data = this.form;
				uni.showLoading({
					title:"加载中...."
				})
				uni.request({
					// url: 'https://unidemo.dcloud.net.cn/api/news',
					url: this.$apiUrl + '/clientAddress/delAddressById',
					method: 'POST',
					header:{
						'Content-type':'application/x-www-form-urlencoded'
					},
					// data: {}, 
					data: {addressid: this.form.id},
					success: res => {
						this.addresses = res.data;
						uni.hideLoading();
					}, 
					fail: () => {},
					complete: () => {}
				});
				
				//不能使用页面跳转，要使用返回页面刷新。不然页在层级过深
				// uni.navigateTo({
				// 	url: "/pages/address/address?is_choose=true"	
				// })
				//返回上一页并刷新
				this.refreshBackPage();
				
				
			},
			refreshBackPage() {
				//不能使用页面跳转，要使用返回页面刷新。不然页在层级过深
				// uni.navigateTo({
				// 	url: "/pages/address/address?is_choose=true"	
				// })
				//返回上一页并刷新
				let pages = getCurrentPages(); // 当前页面
				 let beforePage = pages[pages.length - 2]; // 前一个页面
				 // console.log("beforePage");
				 // console.log(beforePage);
				 uni.navigateBack({
				     success: function() {
				         beforePage.onLoad(); // 执行前一个页面的onLoad方法
				     }
				 });
			}
			
			
		}
	}
</script>

<style lang="scss" scoped>
	.form-box {
		width: 100%;
		height: 100%;
		padding: 30rpx;
		display: flex;
		flex-direction: column;
		color: $text-color-base;
		
		.form-input {
			display: flex;
			align-items: center;
			width: 100%;
		}
		
		.label {
			width: 200rpx;
			font-size: $font-size-lg;
			color: $text-color-base;
			font-weight: 500;
		}
		
		.input {
			flex: 1;
			display: flex;
			align-items: center;
		}
		
		.radio-group {
			display: flex;
			justify-content: flex-start;
			
			.radio {
				padding: 10rpx 30rpx;
				border-radius: 6rpx;
				border: 2rpx solid $text-color-assist;
				color: $text-color-assist;
				font-size: $font-size-base;
				
				&.checked {
					background-color: $color-primary;
					color: $text-color-white;
					border: 2rpx solid $color-primary;
				}
			}
		}
		
		.btn-section {
			flex: 1;
			display: flex;
			align-items: center;
			justify-content: center;
			
			button {
				font-size: $font-size-base;
				height: 90rpx;
				border-radius: 50rem !important;
				width: 85%;
				display: flex;
				align-items: center;
				justify-content: center;
			}
		}
	}
</style>
